package com.xmh.code.generation.filecrate;


import com.xmh.code.generation.inject.Inject;
import com.xmh.code.generation.util.CreateUtils;
import com.xmh.code.generation.util.NameUtils;
import com.xmh.code.generation.vo.ColumnVo;
import com.xmh.code.generation.vo.TableVo;
import org.springframework.stereotype.Component;

import java.util.List;

import static java.lang.String.format;

@Component
public class XmlCreate {

    /**
     * 创建xml文件
     *
     * @param tableNameMaplist
     */
    @SuppressWarnings("unchecked")
    public static void createXml(List<TableVo> tableNameMaplist) {
        for (TableVo tableVo : tableNameMaplist) {
            // 转为驼峰
            String voName = NameUtils.nameTransformation(tableVo.getName());
            // 首字母大写
            String className = NameUtils.titleCase(voName);

            StringBuilder asColumn = new StringBuilder();
            StringBuilder ifLabel = new StringBuilder();
            StringBuilder addColumnUp = new StringBuilder();
            StringBuilder addColumnDown = new StringBuilder();
            StringBuilder updateSetLabelSb = new StringBuilder();

            List<ColumnVo> list = tableVo.getColumnList();

            for (int i = 0; i < list.size(); i++) {
                ColumnVo columnVo = list.get(i);

                String columnName = columnVo.getName();

                String type = columnVo.getType();

                // 字段名转为驼峰
                String cName = NameUtils.nameTransformation(columnName);

                if (i == list.size() - 1) {
                    asColumn.append(format("\t\t\t%s AS %s\n", columnName, cName));
                    addColumnUp.append(format("\t\t\t%s)\n", columnName));
                    addColumnUp.append("		VALUES ( \n");
                    addColumnDown.append(format("\t\t\t#{%s})\n", cName));
                } else {
                    asColumn.append(format("\t\t\t%s AS %s,\n", columnName, cName));
                    addColumnUp.append(format("\t\t\t%s,\n", columnName));
                    addColumnDown.append(format("\t\t\t#{%s},\n", cName));
                }

                String ifStartBelan = null;
                if ("VARCHAR".equals(type)) {
                    ifStartBelan = format("\t\t\t<if test=\"%s != null and %s != '' \">\n", cName, cName);
                } else {
                    ifStartBelan = format("\t\t\t<if test=\"%s != null \">\n", cName);
                }
                String andString = format("\t\t\t\tand %s = #{%s} \n", columnName, cName);
                String ifEndBelan = "			</if>\n";

                ifLabel.append(ifStartBelan);
                ifLabel.append(andString);
                ifLabel.append(ifEndBelan);

                if (!columnName.equals("id")) {
                    updateSetLabelSb.append(ifStartBelan);
                    updateSetLabelSb.append(format("\t\t\t\t%s = #{%s},  \n", columnName, cName));
                    updateSetLabelSb.append(ifEndBelan);
                }
            }
            StringBuilder xmlFileSb = new StringBuilder();

            xmlFileSb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
            xmlFileSb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\n");
            xmlFileSb.append("<mapper namespace=\"__DAO__.__UpperCaseTtableName__DAO\">\n\n");
            xmlFileSb.append("	<select id=\"get\" parameterType=\"___EPath___.__UpperCaseTtableName__Entity\" resultType=\"___EPath___.__UpperCaseTtableName__Entity\">\n");
            xmlFileSb.append("		SELECT \n");
            xmlFileSb.append(asColumn);
            xmlFileSb.append("		FROM _____TableName______ \n");
            xmlFileSb.append("		<where>\n");
            xmlFileSb.append(ifLabel);
            xmlFileSb.append("		</where>\n");
            xmlFileSb.append("	</select>\n\n");
            xmlFileSb.append("	<select id=\"getById\" parameterType=\"java.lang.Integer\" resultType=\"___EPath___.__UpperCaseTtableName__Entity\">\n");
            xmlFileSb.append("		SELECT \n");
            xmlFileSb.append(asColumn);
            xmlFileSb.append("		FROM _____TableName______ \n");
            xmlFileSb.append("		<where>\n");
            xmlFileSb.append("          _____TableName______.id = #{0}\n");
            xmlFileSb.append("		</where>\n");
            xmlFileSb.append("	</select>\n\n");
            xmlFileSb.append("	<insert id=\"add\"  parameterType=\"___EPath___.__UpperCaseTtableName__Entity\" useGeneratedKeys=\"true\" keyProperty=\"id\">\n");
            xmlFileSb.append("		INSERT INTO _____TableName______ (\n");
            xmlFileSb.append(addColumnUp);
            xmlFileSb.append(addColumnDown);
            xmlFileSb.append("	</insert>\n\n");
            xmlFileSb.append("	<delete id=\"delete\" parameterType=\"___EPath___.__UpperCaseTtableName__Entity\">\n");
            xmlFileSb.append("		DELETE FROM _____TableName______\n");
            xmlFileSb.append("		<where>\n");
            xmlFileSb.append(ifLabel);
            xmlFileSb.append("		</where>\n");
            xmlFileSb.append("	</delete>\n\n");
            xmlFileSb.append("	<update id=\"update\" parameterType=\"___EPath___.__UpperCaseTtableName__Entity\">\n");
            xmlFileSb.append("		UPDATE _____TableName______\n");
            xmlFileSb.append("		<set>\n");
            xmlFileSb.append(updateSetLabelSb);
            xmlFileSb.append("		</set>\n");
            xmlFileSb.append("		<where>\n");
            xmlFileSb.append("          id = #{id} \n");
            xmlFileSb.append("		</where>\n");
            xmlFileSb.append("	</update>\n\n");
            xmlFileSb.append("	<select id=\"selectList\" parameterType=\"java.util.Map\" resultType=\"___EPath___.__UpperCaseTtableName__Entity\">\n");
            xmlFileSb.append("		SELECT \n");
            xmlFileSb.append(asColumn);
            xmlFileSb.append("		FROM _____TableName______ \n");
            xmlFileSb.append("		<where>\n");
            xmlFileSb.append(ifLabel);
            xmlFileSb.append("		</where>\n");
            xmlFileSb.append("		<if test=\"pageIndex != null \">\n");
            xmlFileSb.append("			<if test=\"pageNumber != null \">\n");
            xmlFileSb.append("				LIMIT #{pageIndex}, #{pageNumber}\n");
            xmlFileSb.append("			</if>\n");
            xmlFileSb.append("		</if>\n");
            xmlFileSb.append("	</select>\n");
            xmlFileSb.append("</mapper>");

            String xmlCode = xmlFileSb.toString();

            xmlCode = NameUtils.extracted(voName, className, xmlCode);

            xmlCode = xmlCode.replace("_____TableName______", tableVo.getName());

            CreateUtils.outFile(xmlCode, Inject.getXml_Path(), className + "XML", "xml");
        }
    }
}
